import resourcemgApi from "@/api/medicinal";
// 使用非子组件之间的通信进行传递图片的价格地址不成功，故采用跳转路由时，访问后端接口，查询返回对应药材的价格地址
// import {EventBus} from "@/assets/eventBus";
import AddNew from "./form/editForm.vue";
import VueElementLoading from 'vue-element-loading';
import {hasPermissionPoint} from "@/utils/permission"

const components = {
    AddNew,
    VueElementLoading
};

const defaultFormThead = [
    // { en: "mLatinName", cn: "药材拉丁名" },
    // { en: "mOriginName", cn: "基源名称" },
    // { en: "mOriginLatinName", cn: "基源拉丁名" },
    // { en: "mMethod", cn: "药用部位" },
    // { en: "mOriginInterview", cn: "基源简介" },
    // { en: "mRemarks", cn: "药材简介" }
    "mLatinName",
    "mOriginName",
    "mOriginLatinName",
    "mMethod",
    "mOriginInterview",
    "mRemarks"
];

// 对应的中文名，用于给label赋值
const test = {
    "mLatinName": "药材拉丁名",
    "mOriginName": "基源名称",
    "mOriginLatinName": "基源拉丁名",
    "mMethod": "药用部位",
    "mOriginInterview": "基源简介",
    "mRemarks": "药材简介"
};

const data = function () {
    return {
        toMetaTitle: "",
        name: "",
        link: "",
        total: 0, //总记录数
        currentPage: 1, //当前页码
        pageSize: 5, // 每页显示5条数据
        list: [],
        radio: null, // 如果使用单选框，定义一个model值
        currentSelectItem: {}, //当前选中的值
        searchMap: {
            mName: ""
        },
        sum: 0,
        key: 1,
        formTheadOptions: [
            "mLatinName",
            "mOriginName",
            "mOriginLatinName",
            "mMethod",
            "mOriginInterview",
            "mRemarks"
        ],
        fpojo: {
            mName: "",
            mLatinName: "",
            mOriginName: "",
            mOriginLatinName: "",
            mMethod: "",
            mOriginInterview: "",
            mRemarks: "",
            imgPath: "",
            imgPathOut: "",
            mOriginImgPath: "",
            imgOriginPathOut: "",
            mPriceLink: ""
        },
        backup: {},
        test: test,
        checkboxVal: defaultFormThead, // checkboxVal
        formThead: defaultFormThead, // 默认表头 Default header
        // value: this.$store.state.tipstate.value
        // value: false
        editdataDialog: false,   // 编辑中药信息弹窗
        isEdit: false,
        title: "",   // 弹窗标题
    };
};

const watch = {
    checkboxVal(valArr) {
        this.formThead = this.formTheadOptions.filter(
            i => valArr.indexOf(i) >= 0
        );
        this.key = this.key + 1; // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
    }
};

const methods = {

    btnshow(point) {
        return hasPermissionPoint(point);
    },

    /**************************** */
    // 增删查改
    // 查询
    fetchData() {
        resourcemgApi
            .zymanageSearch(this.currentPage, this.pageSize)
            .then(resp => {
                // const resp = response.data;
                this.list = resp.data.rows;
                this.total = resp.data.total;
                console.log(resp);
            });
    },

    // 绑定查询按钮
    searchMedicinal() {
        this.currentPage = 1;
        this.searchBymName();
    },

    // 按照药材名称查询
    searchBymName() {
        var vm = this.$store;
        vm.commit('updateLoading', true);
        console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
        // console.log('是否展开',this.$store.state.app.sidebar.opened)
        resourcemgApi
            .zySearchBymName(this.searchMap.mName, this.currentPage, this.pageSize)
            .then(resp => {
                this.list = resp.data.rows;
                this.total = resp.data.total;
                if (resp.data.rows) {
                    vm.commit('updateLoading', false);
                    console.log('是否开启加载', this.$store.state.elementloading.vueElementLoading)
                }
            });
    },

    // 修改操作
    handleEdit(id) {
        if (id != null && id != "") {
            this.isEdit = true; // 切换为修改状态
            this.opendialog(this.isEdit);
            // this.fpojo
        } else {
            this.$message({
                message: "请选择需要修改的药材哦！",
                type: "warning"
            });
        }
    },

    // 删除操作
    handleDelete(id) {
        if (id != null && id != "") {
            this.$confirm("此操作将永久删除该药材信息, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    resourcemgApi.medicinalDelete(id, this.fpojo.imgPath, this.fpojo.mOriginImgPath).then(resp => {
                        if (resp.data.flag) {
                            this.$message({
                                message: "删除药材信息成功",
                                type: "success"
                            });
                            this.searchBymName();
                            this.radio = '';
                        } else {
                            this.$message({
                                message: "删除药材信息失败",
                                type: "warning"
                            });
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除"
                    });
                });
        } else {
            this.$message({
                message: "请选择需要删除的药材哦！",
                type: "warning"
            });
        }
    },

    /*************************/
    // 操作表格
    // 跳转价格页面
    showprice(link) {
        // this.toMetaTitle = this.list[index].m_name + '价格';
        // alert(this.list[index].m_name);
        // let link = this.list[index].m_price_link;
        // EventBus.$emit('showlink',{
        //   num: 2
        // });
        window.open(link, "_blank");
        // this.$router.push({
        //   name: "zyprice",
        //   // params: { name: this.list[index].m_name },
        //   params: {
        //     link: this.list[index].m_price_link,
        //     name: this.list[index].m_name
        //   }
        // });
    },
    // 双击一行
    openDetails(row) {
        console.log(row.id);
        const mid = row.id;
        this.$store.dispatch('addMid',mid);
        this.$router.push({
            name: "productionarea",
            // query: {
            //     mid: row.id,
            //     name: row.m_name
            // }
            // query: { index: index }
        });
    },
    // 显示索引
    // showIndex(index){
    //   alert(index)
    //   alert(this.list[index].m_price_link)
    // },
    // 单击一行
    chooseone(row) {
        // this.checked = true;
        this.radio = row.id;
        this.fpojo = row;
        // 给imgPathOut、imgOriginPathOut赋值，用于给后端进行判断图片是否更新
        this.fpojo.imgPathOut = row.imgPath;
        this.fpojo.imgOriginPathOut = row.mOriginImgPath;
        this.backup = row;
        if (this.sum == 0) {
            this.$notify({
                title: "温馨提示",
                message: "双击数据行可以查看详情信息哦！",
                type: "success",
                duration: 1000
            });
        }
        this.sum += 1;
    },

    handleSizeChange(val) {
        console.log(`每页${val}条`);
        this.pageSize = val;
        this.searchBymName();
    },
    handleCurrentChange(val) {
        console.log(`当前页${val}`);
        this.currentPage = val;
        this.searchBymName();
    },

    handleSelectionChange(row) {
        console.log(row);
        this.currentSelectItem = row;
    },

    /****************************/
    // 操作弹窗
    // 打开新增弹窗
    openNew() {
        this.isEdit = false;
        this.opendialog(this.isEdit);
    },

    // 打开窗口
    opendialog(e) {
        if (e) {
            this.fpojo = this.backup;
            this.title = "修改药材";
        } else {
            this.title = "新增药材";
            this.fpojo = {
                mLatinName: "",
                mOriginName: "",
                mOriginLatinName: "",
                mMethod: "",
                mOriginInterview: "",
                mRemarks: ""
            };
        }
        this.editdataDialog = true;
        // 等渲染完成之后再清除校验规则
        this.$nextTick(() => {
            this.$refs.feditForm.cancelFieldValidate("seditForm");
        });
    },

    // 关闭弹窗
    closeEditor() {
        this.editdataDialog = false;
        this.fpojo = {
            mLatinName: "",
            mOriginName: "",
            mOriginLatinName: "",
            mMethod: "",
            mOriginInterview: "",
            mRemarks: ""
        };
        this.searchBymName();
    },

    // 关闭之前的回调
    handleDialogClose() {
        this.searchBymName();
        this.editdataDialog = false;
    },

    /*****************************/
    // 操作表单
    // 提交表单
    addData() {
        this.$refs.feditForm.validateForm();
    },

    // 改变提示状态
    // changvalue() {
    //   this.$store.dispatch('close',this.value)
    // }
};

function created() {
    // console.log(this.$store.state.tipstate.value)
    // 开启loading
    this.searchBymName();
};

export default {
    components,
    defaultFormThead,
    test,
    data,
    watch,
    methods,
    created
}